package com.example.studentmanagement.dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.studentmanagement.entity.Curriculum;
import com.example.studentmanagement.util.DBUtil;

import java.util.ArrayList;
import java.util.List;

public class CurriculumDao {

    public static SQLiteDatabase db= DBUtil.db;


    public long addCurriculum(Curriculum curriculum){
        ContentValues contentValues = new ContentValues();
        contentValues.put("cName" ,curriculum.getcName());
        contentValues.put("cId" , curriculum.getcId());
        contentValues.put("cTeacher" , curriculum.getcTeacher());
        contentValues.put("cClass" , curriculum.getcClass());
        contentValues.put("cTime" , curriculum.getcTime());
        long curriculums;
        curriculums = db.insert("c_cur" , null , contentValues);
        return curriculums;
    }

    public String getCourseName(String curriculumId) {
        String[] columns = { "cName" };
        String selection = "cId = ?";
        String[] selectionArgs = { curriculumId };
        String curriculumName = null;
        Cursor cursor = null;
        try {
            cursor = db.query("c_cur", columns, selection, selectionArgs, null, null, null);
            if (cursor.moveToFirst()) {
                curriculumName = cursor.getString(0); // 索引0对应curriculumName
            }
        } catch (Exception e) {
            // 异常处理
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return curriculumName;
    }

    @SuppressLint("Range")
    public Curriculum getCourseDetails(String curriculumId) {
        Curriculum course = null;
        String[] columns = { "cId", "cName", "cTeacher", "cClass", "cTime" };
        String selection = "cId = ?";
        String[] selectionArgs = { curriculumId };
        Cursor cursor = null;
        try {
            cursor = db.query("c_cur", columns, selection, selectionArgs, null, null, null);
            if (cursor.moveToFirst()) {
                // 假设Curriculum是一个实体类，用于封装课程信息
                course = new Curriculum(
                        cursor.getString(cursor.getColumnIndex("cId")),
                        cursor.getString(cursor.getColumnIndex("cName")),
                        cursor.getString(cursor.getColumnIndex("cTeacher")),
                        cursor.getString(cursor.getColumnIndex("cClass")),
                        cursor.getInt(cursor.getColumnIndex("cTime"))
                );
            }
        } catch (Exception e) {
            // 异常处理
        } finally {
            if (cursor != null) {
                cursor.close();
            }
//            db.close();
        }
        return course;
    }

    @SuppressLint("Range")
    public List<Curriculum> getAllCurriculum(){
        List<Curriculum> curriculums = new ArrayList<>();
        Cursor cursor = db.query("c_cur" , new String[]{"cName" , "cId" ,
                        "cTeacher" , "cClass" , "cTime" },
                null , null , null , null , null);
        if (cursor != null && cursor.moveToFirst()){
            do {
                String cName = cursor.getString(cursor.getColumnIndex("cName"));
                String cId = cursor.getString(cursor.getColumnIndex("cId"));
                String cTeacher = cursor.getString(cursor.getColumnIndex("cTeacher"));
                String cClass = cursor.getString(cursor.getColumnIndex("cClass"));
                int cTime = Integer.parseInt(cursor.getString(cursor.getColumnIndex("cTime")));
                curriculums.add(new Curriculum(cName , cId , cTeacher , cClass , cTime));
            }while (cursor.moveToNext());
        }
        if (cursor != null){
            cursor.close();
        }
//        db.close();
        return curriculums ;
    }


}
